/**
 * Created by umakr on 2018/5/8.
 */
import {
  saveOrUpdateLottery ,listLotteryHistory,listAndSaveLotteryResult ,getLotteryInfoById,deleteLottery,listLotteryResultById,informWinUser
} from '../services/lottery';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'lottery',

  state: {
    index:0,
    loading: true,
    checkUserListData:[],
  },

  effects: {
    *fetchSaveOrUpdateLottery({payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(saveOrUpdateLottery,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    },

    *listLotteryHistory({payload,callback}, {call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listLotteryHistory,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    },

    *fetchDelLottery({payload,callback}, { call, put }) {
      const response = yield call(deleteLottery,payload);
      callback(response)
    },

    /**
     * 进入到抽奖活动的 新增 编辑 详情 页面
     */
     *fetchLotteryAdd({payload}, { put }) {
      if(payload.record) {
        yield put(routerRedux.push({
          pathname:'/'+payload.path,
          query:{
            record:payload.record,
            winUserRecord:payload.winUserRecord,
            showDetail:payload.showDetail,
            userListStatus:payload.userListStatus,
          }
        }));
      }else{
        yield put(routerRedux.push({
          pathname:'/'+payload.path
        }));
      }
    },

    *listLotteryResultById({payload,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listLotteryResultById,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    },

    *fetchGetLotteryById({payload ,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getLotteryInfoById,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    },

    *listAndSaveLotteryResult({payload,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listAndSaveLotteryResult,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    },
    *informWinUser({payload,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(informWinUser,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    }
  },


  reducers: {
    listSpreadByType(state, action) {
      return {
        ...state,
        spreadListData: action.payload.list,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  }
};
